//
//  HYIndex.m
//  HaiYue
//
//  Created by skyline on 2018/6/4.
//  Copyright © 2018年 skyline. All rights reserved.
//

#import "HYIndex.h"
@interface HYIndex()
@property (nonatomic, assign, readonly) CGFloat itemHeight;
@property (nonatomic, assign) CGFloat touchPointY;
@property (nonatomic, strong) UIFont *itemFont;
@property (nonatomic, strong) NSArray *items;
@end
@implementation HYIndex

-(instancetype)init
{
    self = [super init];
    if (self)
    {
        self.backgroundColor = [UIColor clearColor];
        CGSize size = [self.items[0] sizeWithAttributes:@{NSAttachmentAttributeName:self.itemFont}];
        self.bounds = CGRectMake(0, 0, ceil(size.width) + 6, (SCREEN_HEIGHT - STATUS_HEIGHT - NAVIBAR_HEIGHT) * 1.0);
        [self addItems];
    }
    return self;
}

- (NSArray *)items
{
    if (!_items)
    {
        _items = [NSArray upperCharacters];
    }
    return _items;
}

- (UIFont *)itemFont
{
    if (!_itemFont)
    {
        _itemFont = [UIFont systemFontOfSize:ContentFont weight:UIFontWeightLight];
    }
    return _itemFont;
}

- (CGFloat)itemHeight
{
    return self.bounds.size.height * 1.0 / self.items.count;
}

- (int)currentIndex
{
    if (self.touchPointY < 0)
        return 0;
    else if (self.touchPointY > self.bounds.size.height)
        return (int)self.items.count;
    else
        return self.touchPointY / self.itemHeight;
}

- (void)addItems
{
    for (int i = 0; i < self.items.count ; i++)
    {
        UILabel *label = [[UILabel alloc] init];
        label.text = self.items[i];
        label.font = self.itemFont;
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = TEXT_BLACK_COLOR;
        
        [self addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self);
            make.right.equalTo(self);
            make.top.equalTo(self).with.offset(self.itemHeight * i);
            make.height.mas_offset(self.itemHeight);
        }];
    }
    
    [self mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_offset(self.bounds.size.width);
    }];
    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
   self.backgroundColor = RGBA(230, 230, 230, 0.5);
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    self.touchPointY = [touch locationInView:self].y;
    [self sendActionsForControlEvents:UIControlEventValueChanged];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.backgroundColor = [UIColor clearColor];
    UITouch *touch = [touches anyObject];
    self.touchPointY = [touch locationInView:self].y;
    [self sendActionsForControlEvents:UIControlEventTouchUpInside];
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
